package com.core;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import com.model.KeyValue;

public class HTMLTagUtils {

    public static String customTag ( String tag,
                                     KeyValueDictionary list,
                                     String text ) {

        String result = "";
        if ( (tag == null) || (tag.isEmpty()) ) {
            return result;
        }
        StringBuilder sb = new StringBuilder();
        Set <Entry <String, Object>> set = list.entrySet();
        Iterator <Entry <String, Object>> iterator = set.iterator();
        while ( iterator.hasNext() ) {
            Entry <String, Object> keyValue = iterator.next();
            String theValue = keyValue.toString();
            if ( theValue != null ) {
                if ( !theValue.isEmpty() ) {
                    sb.append(theValue.concat(" "));
                }
            }
        }

        result = String.format("<%s %s>%s</%s>", tag, sb.toString(), text, tag);

        return result;
    }



    public static String customTag ( String tag,
                                     List <KeyValue> list,
                                     String text ) {

        String result = "";
        if ( (tag == null) || (tag.isEmpty()) ) {
            return result;
        }

        StringBuilder stringBuilder = new StringBuilder();
        Iterator <KeyValue> iterator = list.iterator();
        while ( iterator.hasNext() ) {
            KeyValue keyValue = iterator.next();
            stringBuilder.append(keyValue.toString().concat(" "));
        }

        result = String.format("<%s %s>%s</%s>", tag, stringBuilder.toString(), text, tag);

        return result;

    }



    public static String anchor ( String href,
                                  String style,
                                  String text ) {

        String result = "";
        String ref = "#";
        if ( (href == null) || (href.isEmpty()) ) {
            ref = "#";
        } else {
            ref = href;
        }

        List <KeyValue> attrs = new ArrayList <KeyValue>();
        attrs.add(new KeyValue("class", style, true));
        attrs.add(new KeyValue("href", ref, true));
        result = HTMLTagUtils.customTag("a", attrs, text);

        return result;
    }



    public static String image ( String href,
                                 String style,
                                 String text ) {



        List <KeyValue> attrs = new ArrayList <KeyValue>();
        attrs.add(new KeyValue("class", style, true));
        attrs.add(new KeyValue("src", href, true));

        return HTMLTagUtils.customTag("image", attrs, text);

    }



    public static String h3 ( String style,
                              String text ) {

        String result = "";
        String anchorText = "";
        if ( (text == null) || (text.isEmpty()) ) {
            anchorText = "";
        } else {
            anchorText = text;
        }
        List <KeyValue> attrs = new ArrayList <KeyValue>();
        attrs.add(new KeyValue("class", style, true));
        result = HTMLTagUtils.customTag("div", attrs, anchorText);
        return result;
    }
}
